from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from untitled import Ui_Form
import sys
import cv2 as cv
from PyQt5.QtCore import QCoreApplication
import numpy as np
from PyQt5 import QtCore, QtGui
from predict import pre

class My(QMainWindow, Ui_Form):
    def __init__(self):
        super(My, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('农作物虫害识别')
        self.setIcon()
        self.pushButton.clicked.connect(self.pic)
        self.pushButton_2.clicked.connect(self.pre)

    def setIcon(self):
        palette1 = QPalette()
        # palette1.setColor(self.backgroundRole(), QColor(192,253,123))   # 设置背景颜色
        palette1.setBrush(self.backgroundRole(), QBrush(QPixmap('back.png')))  # 设置背景图片
        self.setPalette(palette1)
    def pre(self):
        out=pre(self.imgName)
        self.label_4.setText(str(out))

    def pic(self):
        self.imgName, imgType = QFileDialog.getOpenFileName(self,
                                                       "打开图片",
                                                       "",
                                                       " *.jpg;;*.png;;*.jpeg;;*.bmp;;All Files (*)")
        png = QtGui.QPixmap(self.imgName).scaled(self.label.width(),self.label.height(), QtCore.Qt.KeepAspectRatio)  # 适应设计label时的大小
        self.label.setPixmap(png)

    def cv_qt(self, src):
        # src必须为bgr格式图像
        # src必须为bgr格式图像
        # src必须为bgr格式图像
        if len(src.shape) == 2:
            src = np.expand_dims(src, axis=-1)
            src = np.tile(src, (1, 1, 3))
            h, w, d = src.shape
        else:
            h, w, d = src.shape

        bytesperline = d * w
        # self.src=cv.cvtColor(self.src,cv.COLOR_BGR2RGB)
        qt_image = QImage(src.data, w, h, bytesperline, QImage.Format_RGB888).rgbSwapped()
        return qt_image


if __name__ == '__main__':
    QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    my = My()
    my.show()
    sys.exit(app.exec_())

